/**
 * 
 */
package pl.koziolweb.wfm.server.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author koziolek
 * 
 */
public class ConfigurationLoadaer {

	private static final ConfigurationLoadaer INSTANCE = new ConfigurationLoadaer();

	private Properties propertires;

	private ConfigurationLoadaer() {
		propertires = new Properties();
		try {
			propertires.load(getPropertiesFile());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @return
	 * @throws IOException
	 */
	private FileInputStream getPropertiesFile() throws IOException {
		String path = "";
		String wfmHome = System.getenv("WFM_HOME");
		if (wfmHome != null) {
			path = wfmHome;
		}
		File props = new File(path + "/wfm.properties");
		if (!props.exists()) {
			createDefaultProps(props);
		}
		return new FileInputStream(props);
	}

	private void createDefaultProps(File props) throws IOException {
		props.createNewFile();
		Properties properties = new Properties();
		properties.setProperty("system.root", ".");
		properties.setProperty("correct.files.extensions", "");
		properties
				.store(new FileOutputStream(props),
						"This is default wfm.properties file. Please fill it yourself.");

	}

	public static ConfigurationLoadaer getINSTANCE() {
		return INSTANCE;
	}

	public String getProperty(String key) {
		return propertires.getProperty(key);
	}
}
